import http from '../http'
import qs from 'qs'
import { request, requestOnline, ResponseCode, moment } from '../baseUrl'

/**
 *  @param request 请求地址 例如：http://197.82.15.15:8088/request/...
 *  @param '/testIp'代表vue-cil中config，index.js中配置的代理
 */

/**
 * @func 查询应用列表
 */
const appList = `/app/list`
export function getAppList({ pageNo = 1, pageSize = 100, appName, partnership = '1' }) {
  return http.get(`${requestOnline}${appList}?${qs.stringify({ pageNo, pageSize, appName, partnership })}`)
}

/**
 * @func 查询应用数据列表
 * @param { 加载列表 查询列表 }
 */
const appData = `/app/data`
export async function getAppData({
  pageNo = 1,
  pageSize = 100,
  id = '',
  key = '',
  name,
  startTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .add(1, 'days')
    .valueOf(),
}) {
  const response = await http.get(`${requestOnline}${appData}?${qs.stringify({ pageNo, pageSize, id, key, name, startTime, endTime })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 查询广告位列表
 * @param { 查询列表 }
 */
const slotList = `/slot/list`
export async function getSlotList({ pageNo = 1, pageSize = 100, appKey = '', slotType = 0, appName, slotName }) {
  const response = await http.get(`${requestOnline}${slotList}?${qs.stringify({ pageNo, pageSize, appKey, slotType, appName, slotName })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 置顶广告位
 * @param { id }
 */
const slotTop = `/slot/top`
export async function postSlotTop(slotKey) {
  const response = await http.post(`${requestOnline}${slotTop}`, qs.parse(slotKey))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 取消置顶广告位
 * @param { id }
 */
const slotUnTop = `/slot/unTop`
export async function postSlotUnTop(slotKey) {
  const response = await http.post(`${requestOnline}${slotUnTop}`, qs.parse(slotKey))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 查询广告位数据列表
 * @param { 查询列表 }
 */
const slotData = `/slot/data`
export async function getSlotData({
  pageNo,
  pageSize,
  key,
  id = '',
  name,
  advertType,
  startTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .add(1, 'days')
    .valueOf(),
}) {
  const response = await http.get(
    `${requestOnline}${slotData}?${qs.stringify({ pageNo, pageSize, id, name, key, advertType, startTime, endTime })}`,
  )
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 新增或修改应用
 */
const appUpdate = `/app/update`
export async function postAppUpdate(param, appKey) {
  const response = await http.post(`${requestOnline}${appUpdate}`, qs.parse(param, appKey))
  if (![ResponseCode.success].includes(response.data.code)) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @子账号列表
 */
const subUserList = `/subUser/list/app`
export async function getSubUserChildList() {
  const response = await http.get(`${requestOnline}${subUserList}`)
  if (![ResponseCode.success].includes(response.data.code)) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @活动列表
 */
const catList = `/act`
export async function getCatList() {
  const response = await http.get(`${requestOnline}${catList}`)
  if (![ResponseCode.success].includes(response.data.code)) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 查询广告投放计划列表
 */
const adverPlanList = `/advertPlan/list`
export async function getAdverPlanData({ pageNo, pageSize, types }) {
  const response = await http.get(`${requestOnline}${adverPlanList}?${qs.stringify({ pageNo, pageSize, types })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 新增广告位
 */
const slotUpdate = `/slot/update`
export async function addSlotUpdate(param) {
  const response = await http.post(`${requestOnline}${slotUpdate}`, qs.parse(param))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}
/**
 * @func 下架广告位
 */
const slotUnshow = `/slot/unShow`
export async function soldSlotUpShow(param) {
  const response = await http.post(`${requestOnline}${slotUnshow}`, qs.parse(param))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}
/**
 * @func 查询收入列表
 */

const incomeList = `/slot/income/list`
export async function getIncomeList({ pageNo, pageSize, sk }) {
  const response = await http.get(`${requestOnline}${incomeList}?${qs.stringify({ pageNo, pageSize, sk })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}
/**
 * @func 新增收入列表
 */

const addIncomeList = `/slot/income/add`
export async function AddIncomeList(param) {
  const response = await http.post(`${requestOnline}${addIncomeList}`, qs.parse(param))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 查询指定广告位广告投放计划列表 // 优先计划
 */

const adverList = `/slot/sorted/advert/list`
export async function getAdberList({ sk }) {
  const response = await http.get(`${requestOnline}${adverList}?${qs.stringify({ sk })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}
const slotadverList = `/slot/advert/list`
export async function getSlotAdberList({ sk, advertType = 0 }) {
  const response = await http.get(`${requestOnline}${slotadverList}?${qs.stringify({ sk, advertType })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

const editAdverList = `/slot/advert/sort`
export async function postSlotAdberList(param) {
  const response = await http.post(`${requestOnline}${editAdverList}`, qs.parse(param))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 媒体数据-列表嵌套表格数据
 * @param {列表对象: record}
 */
const appDataDay = `/app/data/day`
export async function getAppDataDay({
  key,
  startTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .add(1, 'days')
    .valueOf(),
}) {
  try {
    const response = await http.get(`${requestOnline}${appDataDay}?${qs.stringify({ key, startTime, endTime })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 广告位数据-列表嵌套表格数据
 * @param {列表对象: record}
 */
const slotDataDay = `/slot/data/day`
export async function getSlotDataDay({
  key,
  startTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .add(1, 'days')
    .valueOf(),
}) {
  try {
    const response = await http.get(`${requestOnline}${slotDataDay}?${qs.stringify({ key, startTime, endTime })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 查询媒体收入数据列表
 * @param { 加载列表 查询列表 }
 */
const incomeApp = `/income/app`
export async function getIncomeApp({
  pageNo = 1,
  pageSize = 10,
  startTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
}) {
  try {
    const response = await http.get(`${requestOnline}${incomeApp}?${qs.stringify({ pageNo, pageSize, startTime, endTime })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 媒体收入数据-列表嵌套表格数据
 * @param {列表对象: record}
 */
const incomeAppDay = `/income/app/day`
export async function getIncomeAppDay({
  appKey,
  startTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
}) {
  try {
    const response = await http.get(`${requestOnline}${incomeAppDay}?${qs.stringify({ appKey, startTime, endTime })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 查询广告位收入数据列表
 * @param { 加载列表 查询列表 }
 */
const incomeSlot = `/income/slot`
export async function getIncomeSlot({
  pageNo = 1,
  pageSize = 10,
  startTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
}) {
  try {
    const response = await http.get(`${requestOnline}${incomeSlot}?${qs.stringify({ pageNo, pageSize, startTime, endTime })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 媒体收入数据-列表嵌套表格数据
 * @param {列表对象: record}
 */
const incomeSlotDay = `/income/slot/day`
export async function getIncomeSlotDay({
  sk,
  startTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(1, 'days')
    .valueOf(),
}) {
  try {
    const response = await http.get(`${requestOnline}${incomeSlotDay}?${qs.stringify({ sk, startTime, endTime })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}
